<template><div><h2 id="发送邮件" tabindex="-1"><a class="header-anchor" href="#发送邮件"><span>发送邮件</span></a></h2>
<p>Yii 支持电子邮件信息的合成和发送。然而，框架核心只提供内容组合功能和基本接口。实际的邮件发送机制应该由扩展提供，因为不同的项目可能需要不同的实现，它通常依赖于外部服务和库。</p>
<p>对于最常见的情况，你可以使用 <a href="https://github.com/yiisoft/yii2-swiftmailer" title="yii2-swiftmailer" target="_blank" rel="noopener noreferrer"><code v-pre>yii2-swiftmailer</code></a> 官方扩展，一般在 Yii2 框架应用模板安装后就自动安装了这个扩展。</p>
<p>当然你可以使用 <a href="https://github.com/yiisoft/yii2-symfonymailer" title="yii2-symfonymailer" target="_blank" rel="noopener noreferrer"><code v-pre>yii2-symfonymailer</code></a> 官方扩展。这个扩展为 Yii 框架 2.0 提供了一个 Symfony Mailer 邮件解决方案，在本文后面的内容中，都将介绍。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>邮件组件配置取决于您所选择的扩展。一般来说，你的应用程序配置应该是这样的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\swiftmailer\Mailer'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 包含用于撰写邮件消息的视图文件的目录，默认值是 '@app/mail'</span></span>
<span class="line">            <span class="token string single-quoted-string">'viewPath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@common/mail'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 'useFileTransport' => true,</span></span>
<span class="line">            <span class="token comment">// 'useFileTransport' 的默认值为 true，即将所有邮件发送到一个文件。你必须将它设置为 false，并为邮件发送者配置一个传输来发送真实的邮件。</span></span>
<span class="line">            <span class="token string single-quoted-string">'useFileTransport'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 当 'useFileTransport' 为 false 时，才配置。</span></span>
<span class="line">            <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Swift_SmtpTransport'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'encryption'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'tls'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your_mail_server_host'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your_smtp_port'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your_username'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your_password'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以跳过 <code v-pre>transport</code> 属性的配置。在这种情况下，默认的 <code v-pre>\Swift_SendmailTransport</code> 传输将被用于发送电子邮件。</p>
<p>你在配置中使用 ‘constructArgs’ 键，来指定传输构造函数参数。您还可以指定插件列表，这些插件应该使用 ‘plugins’ 键，来注册到传输。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Swift_SmtpTransport'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'constructArgs'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'plugins'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Swift_Plugins_ThrottlerPlugin'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'constructArgs'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>一旦 <code v-pre>mailer</code> 组件被配置，你就可以使用下面的代码来发送电子邮件消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from@domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'to@domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Message subject'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setTextBody</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Plain text content'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setHtmlBody</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;b>HTML content&lt;/b>'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的例子中，<code v-pre>compose()</code> 方法创建了一个邮件消息的实例，然后填充并发送该邮件消息。如果需要，你可以在这个过程中加入更复杂的逻辑，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 游客</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">isGuest</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">setFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from@domain.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">setFrom</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">identity</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">setTo</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">params</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'adminEmail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Message subject'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setTextBody</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Plain text content'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><img src="https://cdn.learnku.com/assets/images/emoji/information_source.png" alt=":information_source:"> 注意：每个 <code v-pre>mailer</code> 扩展有两大类：<code v-pre>Mailer</code> 和 <code v-pre>Message</code>。前者总是知道后者的类名和具体内容。不要尝试直接实例化 <code v-pre>Message</code> 对象，而是一如既往地使用 <code v-pre>compose()</code> 方法接口。</p>
</blockquote>
<h2 id="批量发送邮件" tabindex="-1"><a class="header-anchor" href="#批量发送邮件"><span>批量发送邮件</span></a></h2>
<p>你也可以一次同时发送多条邮件消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$messages</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">setTo</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">sendMultiple</span><span class="token punctuation">(</span><span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一些特定的邮件扩展可以从这种方法中受益，比如，使用单一的网络消息等。</p>
<h2 id="撰写邮件内容" tabindex="-1"><a class="header-anchor" href="#撰写邮件内容"><span>撰写邮件内容</span></a></h2>
<p>Yii 允许你通过一些特殊的视图文件，来组合实际的邮件消息内容。默认情况下，这些文件应该位于 <code v-pre>@app/mail</code> 路径下，高级模板一般位于 <code v-pre>@common/mail</code> 下。邮件视图文件内容的示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Html</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Url</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/* @var $this \yii\web\View view component instance */</span></span>
<span class="line"><span class="token comment">/* @var $message \yii\mail\BaseMessage instance of newly created mail message */</span></span>
<span class="line"></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>This message allows you to visit our site home page by one click<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Go to home page'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">home</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In order to compose message content via view file simply pass view name to the <code v-pre>compose()</code> method:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'home-link'</span><span class="token punctuation">)</span> <span class="token comment">// a view rendering result becomes the message body here</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from@domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'to@domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setSubject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Message subject'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may pass additional view parameters to <code v-pre>compose()</code> method, which will be available inside the view files:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'greetings'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">identity</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'advertisement'</span> <span class="token operator">=></span> <span class="token variable">$adContent</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You can specify different view files for HTML and plain text message contents:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'contact-html'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'text'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'contact-text'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你将视图名指定为一个标量字符串，它的呈现结果将被用作 HTML 主体，而纯文本主体将通过从 HTML 中删除所有的 HTML 实体来组成。</p>
<p>视图渲染结果可以包装到布局（layout）中，可以使用 <code v-pre>yii\mail\BaseMailer::htmlLayout</code> 和 <code v-pre>yii\mail\BaseMailer::textLayout</code> 来设置。它的工作方式与常规 web 应用中的布局相同。布局可以用来设置邮件 CSS 样式或其他共享内容，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Html</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/* @var $this \yii\web\View view component instance */</span></span>
<span class="line"><span class="token comment">/* @var $message \yii\mail\MessageInterface the message being composed */</span></span>
<span class="line"><span class="token comment">/* @var $content string main view render result */</span></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">beginPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span> <span class="token name">PUBLIC</span> <span class="token string">"-//W3C//DTD XHTML 1.0 Strict//EN"</span> <span class="token string">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/1999/xhtml<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Content-Type<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/html; charset=<span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">charset</span> <span class="token delimiter important">?></span></span><span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/css<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css"></span>
<span class="line">        <span class="token selector">.heading</span> <span class="token punctuation">{</span>...<span class="token punctuation">}</span></span>
<span class="line">        <span class="token selector">.list</span> <span class="token punctuation">{</span>...<span class="token punctuation">}</span></span>
<span class="line">        <span class="token selector">.footer</span> <span class="token punctuation">{</span>...<span class="token punctuation">}</span></span>
<span class="line">    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">head</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">beginBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$content</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>footer<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>With kind regards, <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">name</span> <span class="token delimiter important">?></span></span> team<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">endBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">endPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="文件附件" tabindex="-1"><a class="header-anchor" href="#文件附件"><span>文件附件</span></a></h2>
<p>您可以使用方法 <code v-pre>attach()</code> 和 <code v-pre>attachContent()</code> 向邮件消息中添加附件，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 从本地文件系统附加文件</span></span>
<span class="line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/source/file.pdf'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 动态创建附件</span></span>
<span class="line"><span class="token variable">$message</span><span class="token operator">-></span><span class="token function">attachContent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Attachment content'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'fileName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'attach.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'contentType'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'text/plain'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="插入图片" tabindex="-1"><a class="header-anchor" href="#插入图片"><span>插入图片</span></a></h2>
<p>你可以使用 <code v-pre>embed()</code> 方法将图像嵌入到邮件消息内容中。此方法返回附件 ID，然后在 <code v-pre>img</code> 标签中使用该 ID。当通过视图文件编写消息内容时，这个方法很容易使用，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'embed-email'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'imageFileName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/path/to/image.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后在邮件视图文件中，你可以使用以下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">embed</span><span class="token punctuation">(</span><span class="token variable">$imageFileName</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token punctuation">"</span></span> <span class="token attr-name">alt</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试与调试" tabindex="-1"><a class="header-anchor" href="#测试与调试"><span>测试与调试</span></a></h2>
<p>开发人员经常需要检查应用程序发送了哪些邮件，邮件内容是什么等等。这种能力由 Yii 通过 <code v-pre>yii\mail\BaseMailer::useFileTransport</code> 属性授予。如果这个属性为 true，此选项强制将邮件消息数据保存到本地文件中，而不是常规发送。这些文件将保存在 <code v-pre>yii\mail\BaseMailer::fileTransportPath</code> 属性值表示的路径下，默认为 <code v-pre>@runtime/mail</code>。</p>
<blockquote>
<p>注意：您可以将消息保存到文件中，也可以将它们发送到实际的收件人，但不能同时做这两件事。</p>
</blockquote>
<p>邮件消息文件可以由常规文本文件编辑器打开，这样您就可以浏览实际的消息头、内容等。在调试应用程序或运行单元测试时，可以证明这种机制本身。</p>
<blockquote>
<p>注意：邮件消息文件内容是通过 <code v-pre>\yii\mail\MessageInterface::toString()</code> 组成的，所以它取决于你在应用程序中使用的实际邮件扩展名。</p>
</blockquote>
<h2 id="symfony-mailer-扩展" tabindex="-1"><a class="header-anchor" href="#symfony-mailer-扩展"><span>Symfony Mailer 扩展</span></a></h2>
<p>这个扩展为 Yii 框架 2.0 提供了一个 Symfony Mailer 邮件解决方案。</p>
<h3 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h3>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> yiisoft<span class="token operator">/</span>yii2<span class="token operator">-</span>symfonymailer</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者在您的 <code v-pre>composer.json</code> 文件中的 require 部分增加以下内容：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token property">"yiisoft/yii2-symfonymailer"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后执行 <code v-pre>composer install</code> 命令即可。</p>
<h3 id="用法" tabindex="-1"><a class="header-anchor" href="#用法"><span>用法</span></a></h3>
<p>要使用这个扩展，只需在您的应用程序配置中添加以下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>symfonymailer<span class="token punctuation">\</span>Mailer</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'scheme'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smtps'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">465</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'native://default'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'viewPath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@common/mail'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// send all mails to a file by default. You have to set</span></span>
<span class="line">            <span class="token comment">// 'useFileTransport' to false and configure transport</span></span>
<span class="line">            <span class="token comment">// for the mailer to send real emails.</span></span>
<span class="line">            <span class="token string single-quoted-string">'useFileTransport'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>symfonymailer<span class="token punctuation">\</span>Mailer</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'transport'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'smtp://user:pass@smtp.example.com:25'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后你可以发送电子邮件，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mailer</span><span class="token operator">-></span><span class="token function">compose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'contact/html'</span><span class="token punctuation">)</span></span>
<span class="line">     <span class="token operator">-></span><span class="token function">setFrom</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from@domain.com'</span><span class="token punctuation">)</span></span>
<span class="line">     <span class="token operator">-></span><span class="token function">setTo</span><span class="token punctuation">(</span><span class="token variable">$form</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span></span>
<span class="line">     <span class="token operator">-></span><span class="token function">setSubject</span><span class="token punctuation">(</span><span class="token variable">$form</span><span class="token operator">-></span><span class="token property">subject</span><span class="token punctuation">)</span></span>
<span class="line">     <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义邮件解决方案" tabindex="-1"><a class="header-anchor" href="#自定义邮件解决方案"><span>自定义邮件解决方案</span></a></h2>
<p>为了创建您自己的自定义邮件解决方案，您需要创建两个类：一个用于 <code v-pre>Mailer</code>，另一个用于 <code v-pre>Message</code>。您可以使用 <code v-pre>yii\mail\BaseMailer</code> 和 <code v-pre>yii\mail\BaseMessage</code> 作为你的解决方案的基类。这些类已经包含了本文档中描述的基本逻辑。但是，它们的使用不是强制性的，只要实现 <code v-pre>yii\mail\MailerInterface</code> 和 <code v-pre>yii\mail\MessageInterface</code> 接口就足够了。然后，您需要实现所有抽象方法来构建您的解决方案。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">common<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mail<span class="token punctuation">\</span>BaseMailer</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CustomMailer</span> <span class="token keyword">extends</span> <span class="token class-name">BaseMailer</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">common<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mail<span class="token punctuation">\</span>BaseMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CustomMessage</span> <span class="token keyword">extends</span> <span class="token class-name">BaseMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">common<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Component</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mail<span class="token punctuation">\</span>MailerInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>ViewContextInterface</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CustomMailer</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token keyword">implements</span> <span class="token class-name">MailerInterface</span><span class="token punctuation">,</span> ViewContextInterface</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">common<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>BaseObject</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mail<span class="token punctuation">\</span>MessageInterface</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CustomMessage</span> <span class="token keyword">extends</span> <span class="token class-name">BaseObject</span> <span class="token keyword">implements</span> <span class="token class-name">MessageInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


